package com.example.service.usr.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.context.PageResult;
import com.example.entity.CartDO;
import com.example.entity.ProductDO;
import com.example.exception.exception;
import com.example.mapper.CartMapper;
import com.example.service.usr.CartService;
import com.example.service.usr.ProductService;
import com.example.vo.cart.CartPageReq;
import com.example.vo.cart.CartReq;
import com.example.vo.cart.CartResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zbxComputer
 * @description 针对表【sys_store_cart(购物车表)】的数据库操作Service实现
 * @createDate 2024-11-21 12:48:44
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, CartDO>
        implements CartService {
    @Resource
    private CartMapper cartMapper;
    @Resource
    private ProductService productService;

    @Override
    public void batchDelete(Long[] ids) {
        removeByIds(List.of(ids));
    }

    @Override
    public PageResult<CartResp> getCart(CartPageReq req) {
        Page<CartResp> page = new Page<>(req.getPage(), req.getSize());
        IPage<CartResp> iPage = cartMapper.getCart(page, req, StpUtil.getLoginIdAsLong());
        return PageResult.of(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public void insertOrUpdateCart(CartReq req) {
        ProductDO productDO = productService.getById(req.getProductId());
        if (productDO == null) {
            throw new exception("商品不存在");
        }
        LambdaQueryWrapper<CartDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CartDO::getUid, getUserId())
                .eq(CartDO::getProductId, req.getProductId());
        CartDO cart = cartMapper.selectOne(wrapper);
        if (cart == null) {
            cart = BeanUtil.copyProperties(req, CartDO.class);
            cart.setUid(getUserId());
            int insert = cartMapper.insert(cart);
            if (insert != 1) {
                throw new exception("添加购物车失败");
            }
        } else {
            if (cart.getProductNum() < 2 && req.getProductNum() < 0) {
                if (!removeById(req.getId())) {
                    throw new exception("删除购物车商品失败");
                }
            } else {
                LambdaUpdateWrapper<CartDO> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(CartDO::getId, cart.getId())
                        .eq(CartDO::getProductNum, cart.getProductNum());
                updateWrapper.set(CartDO::getProductNum, cart.getProductNum() + req.getProductNum());
                int update = cartMapper.update(null, updateWrapper);
                if (update != 1) {
                    throw new exception("更新购物车失败");
                }
            }
        }
    }

    @Override
    public void updateCartNum(CartReq req) {
        LambdaUpdateWrapper<CartDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CartDO::getUid, getUserId())
                .and(q -> q.eq(CartDO::getProductId, req.getProductId()).or().eq(CartDO::getId, req.getId()));
        wrapper.set(CartDO::getProductNum, req.getProductNum());
        int update = cartMapper.update(null, wrapper);
        if (update != 1) {
            throw new exception("更新购物车失败");
        }
    }

    private Long getUserId() {
        return StpUtil.getLoginIdAsLong();
    }
}




